﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using ECCIT;
using ECCIT.Net;
using ECCIT.Utilities;
using OgilvyOne.UKNow.Model;

namespace OgilvyOne.UKNow.BLL
{
    public class QQLoginBLL
    {
        public static ResultInfo GetOpenID(string accessToken)
        {
            string uri = "https://graph.qq.com/oauth2.0/me";
            OAuthAccessToken token = new OAuthAccessToken();
            token.Token = accessToken;
            ResultInfo result = new ResultInfo(false);
            QQOpenIDModel model = null;
            try
            {
                var requester = new OAuthHttpGet(uri, token);
                var response = requester.Request();
                model = DataContractJsonUtil.JsonToObject<QQOpenIDModel>(response);
                result.Result = true;
                result.ReturnObject = model;
            }
            catch (Exception ex)
            {
                result.Message = string.Format("Error occurred in GetOpenID. Message: {0}", ex.Message);
            }

            return result;
        }

        public static QQUserModel GetUserInfo(string accessToken, string appKey, string openID)
        {
            string uri = "https://graph.qq.com/user/get_user_info";
            OAuthAccessToken token = new OAuthAccessToken();
            token.Token = accessToken;
            var requester = new OAuthHttpGet(uri, token);
            requester.Params.Add("oauth_consumer_key", appKey);
            requester.Params.Add("openid", openID);
            requester.Params.Add("format", "json");

            var response = requester.Request();
            var model = DataContractJsonUtil.JsonToObject<QQUserModel>(response);

            return model;
        }
    }
}
